package com.example.demo.cdesign;

import com.example.demo.cdesign.web.CloseRserveProcess;
import com.example.demo.cdesign.web.StartRserver;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

import static com.example.demo.cdesign.CloseRserveProcessWin.findProcess;

public class Test {
    public static Properties props = System.getProperties();
    public static String[] getFpd() {
        String srcPath = "/Users/wangdeying/Downloads/RWD/java_R/totaldata_bynow_remodellneed.csv";
        String charset = "utf-8";
        List<String> lstr = new ArrayList<>();
        try (CSVReader csvReader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(new FileInputStream(new File(srcPath)), charset))).build()) {
            Iterator<String[]> iterator = csvReader.iterator();
            while (iterator.hasNext()) {
                lstr.add(iterator.next()[1]);
//                Arrays.stream(iterator.next()).forEach(System.out::print);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String[] strs = new String[lstr.size()];
        String[] lsTostrs = lstr.toArray(strs);
        Arrays.sort(lsTostrs, Collections.reverseOrder());
        return lsTostrs;
    }

    public static void reverse() {
        Integer[] A = {10, 23, 42, 12, 20, 6};
        Arrays.sort(A, Collections.reverseOrder());
        for (int a : A) {
            System.out.println(a);
        }
    }

    //public static void main(String[] args) throws REngineException, REXPMismatchException {
    //    RConnection rc = new RConnection();
    //    String WD = "C:/Users/wangdeying/Downloads/RWD/test/";
    //    String fpd_from = "5100";
    //    String fpd_end = "5120";
    //    String unitCode = "'TQNPC-1'";
    //    String modelStart = "A09";
    //    String modelEnd = "A10";
    //    String taskId = "20190929";
    //    rc.eval("setwd(\"" + WD + "\")");//指定workdirectory(工作环境)。
    //    rc.eval("load(\"./java_R.RData\")");
    //    rc.eval("print(688878787)");
    //    rc.eval("print(688878787)");
//
    //    rc.eval("source(\"./ToSQL.R\",encoding = \"UTF-8\")");
    //    rc.eval("save.image(\"./java_R.RData\")");//保存RData
    //    rc.eval("source(\"./Remodelling.R\",encoding = \"UTF-8\")");
    //    REXP rexp_rem = rc.eval("Remodelling(local_data = TRUE"
    //            + ",local_path = \"" + WD + "totaldata_bynow_remodellneed.csv\"" +
    //            ",save_rdata = FALSE,model_start=\""  + modelStart + "\",model_end=\"" + modelEnd + "\",task_ID=\"" + taskId + "\")");
//
    //    System.out.println("-success-");
    //    rc.close();
//
    //}
    public static boolean start() {

        if(props.getProperty("os.name").contains("Windows")){
            findProcess("Rserve.exe");
            System.out.println("yes");
        }
        else {
            List<String> PIDS = CloseRserveProcess.getPID("Rserve");
            CloseRserveProcess.closeLinuxProcess(PIDS);
        }
        return StartRserver.checkLocalRserve();
    }

    public static void main(String[] args) {

    }
}
